package com.lxhll.personnel.core;

import com.lxhll.personnel.common.JwtUtil;
import com.lxhll.personnel.entity.Employee;
import com.lxhll.personnel.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

@Component
public class H implements HandlerMethodArgumentResolver {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 判定当前方法上的注解是@CurrentEmployee，并且返回类型是Employee,true交给resolveArgument，否则不处理
     */
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(CurrentEmployee.class) &&
                parameter.getParameterType().isAssignableFrom(Employee.class);
    }

    /**
     * 获取当前用户
     */
    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
                                  NativeWebRequest request, WebDataBinderFactory factory) throws Exception{
        // header中获取用户token
        String token = request.getHeader("authorization");
        Integer employeeId = JwtUtil.verify(token);
        Employee employee=employeeService.getEmployeeById(employeeId);
        return employee;
    }



}
